
<script>
   $(document).ready(function (){

    $('#teamtab a:first').tab('show');

    $('#teamtab a').click(function (e) {
    e.preventDefault();
    $(this).tab('show');
    });

    var amount_selected = $("input:checkbox.select_checkbox:checked").size();
    if (amount_selected >= 20) {
      $("input.select_checkbox:not(:checked)").attr('disabled', true);
    };

    var amount_selected = $("ol#replacements-list li").size();
    if (amount_selected >= 5) {
      $("input.replacement_checkbox:not(:checked)").attr('disabled', true);
    };

    $("a#uncheck_all").click(function() {
      $("input:checkbox").removeAttr('checked').removeAttr('disabled');
    });

    $('h4.teamhead').click(function() {
      $(this).next().toggle('blind');
      return false;
    }).next().hide();


    $("#selection_form").submit(function() {
        //e.preventDefault();
        $("ol#selection-list").empty().append("<img src='<?php echo base_url();?>assets/images/ajaxload.gif' />");
        $("ol#replacements-list").empty().append("<img src='<?php echo base_url();?>assets/images/ajaxload.gif' />");
        $.post(
            "<?php echo site_url('selection/ajax_save_selection'); ?>",
            $(this).serialize() ,
            function(data) {
              var lists = $.parseJSON(data);
              $("ol#selection-list").empty().append(lists.selections);
              $("ol#replacements-list").empty().append(lists.replacements);
            }
          );
       
        return false;
        });

    $("input:checkbox.select_checkbox").click(function() {
      var rider_id = $(this).attr('name');
      var str = rider_id.substr(rider_id.lastIndexOf("["));
      var len = str.length;
      var id = str.slice(1,len-1);
      $("input:checkbox[name='replacement_rider[" + id + "]']").removeAttr('checked');
      var amount_selected = $("input:checkbox.select_checkbox:checked").size();
      if (amount_selected >= 20) {
        $("input.select_checkbox:not(:checked)").attr('disabled', true);
      };
      if (amount_selected < 20) {
        $("input.select_checkbox:not(:checked)").removeAttr('disabled');
      };      
    });

    $("input:checkbox.replacement_checkbox").click(function() {
      var rider_id = $(this).attr('name');
      var str = rider_id.substr(rider_id.lastIndexOf("["));
      var len = str.length;
      var id = str.slice(1,len-1);
      $("input:checkbox[name='select_rider[" + id + "]']").removeAttr('checked');
      var amount_selected = $("input:checkbox.replacement_checkbox:checked").size();
      if (amount_selected >= 5) {
        $("input.replacement_checkbox:not(:checked)").attr('disabled', true);
      };
      if (amount_selected < 5) {
        $("input.replacement_checkbox:not(:checked)").removeAttr('disabled');
      };        
    });
  });   
</script>                    

      <div class='row'>
          
            <div class="span4">
              <h3>Team: <?php echo $user->username; ?></h3>
              <ol id='selection-list'>

              <?php
              if(isset($selection[0]->number))
              {  
              foreach($selection as $sel_rider)
              {
                  $class = 'rider';
                  if (!$sel_rider->active)
                  {
                    $class .= " inactive";
                  }
              ?>  
                  <li class="<?php echo $class;?>">  
                    <?php echo anchor('info/rider/'.$sel_rider->rider_id,$sel_rider->last_name." ".$sel_rider->first_name);?>
                    <?php echo "(".anchor('info/team/'.$sel_rider->team_id, $sel_rider->team_shortname).")";?>
                  </li>
              <?php } 
              }
              else
              { ?>
                <div class="hideme"><?php echo lang('no_selection_yet');?></div>
               <?php 
              } ?>
              </ol>  
            
              <h5><?php echo lang('replacement_riders');?></h5>
         
              <ol id='replacements-list'>
                  
                <?php
                if(isset($replacements[0]->number))
                {  
                foreach($replacements as $rep_rider)
                {
                    $class = 'rider';
                    if (!$rep_rider->active)
                    {
                      $class .= " inactive";
                    }
                ?>  
                    <li class="<?php echo $class;?>">  
                      <?php echo anchor('info/rider/'.$rep_rider->rider_id,$rep_rider->last_name." ".$rep_rider->first_name);?>
                      <?php echo "(".anchor('info/team/'.$rep_rider->team_id, $rep_rider->team_shortname).")";?>
                    </li>
                <?php } 
                }
                else
                { ?>
                  <div class="hideme"><?php echo lang('no_selection_yet');?></div>
                 <?php 
                } ?>

                </ol>
              </div>

    <?php echo form_open('selection/edit', 'id="selection_form"');?>
    <?php echo form_hidden('user',$user->id);?>
    <div class='span8'>
    
    <div class='panel'>
      <div class="tabbable">
      <h4 style='margin-bottom:20px; text-align:center;'><?php echo lang('select_a_team');?></h4>
      <div class="btn-toolbar">
        <div class="btn-group">
          <?php foreach($teams as $team) { ?>
          <a href="#tab<?php echo $team->team_id;?>" data-toggle="tab" class='btn' rel="tooltip" style="font-family:monospace;" title="<?php echo $team->team_name;?>"><?php echo $team->team_shortname;?></a>
          <?php } ?>
        </div>
      </div>
      <hr/>
    </div>

    <div class="tab-content">
        <?php foreach($teams as $team) { ?>
        <div class='tab-pane fade' id="tab<?php echo $team->team_id;?>">
        <h4 style='margin-bottom:20px;'><?php echo $team->team_name;?></h4>  
          <table class='table table-condensed'>
          <thead>
            <tr><th><?php echo lang('rider');?></th><th></th><th class='centertext'>Select</th><th class='centertext'>Reserve</th></tr>
          </thead>
          <tbody>
          <?php foreach ($team->team_riders as $rider) { ?>

            <tr><td><?php echo $rider->number." - ".$rider->last_name." ".$rider->first_name; ?></td>
            <td><?php echo lang($rider->type_description);?></td>
            <td class='centertext'>
                <?php
                $data = array(
                    'name'        => 'select_rider['.$rider->rider_id.']',
                    'id'          => 'select_rider',
                    'value'       => '1',
                    'checked'     => in_array($rider->rider_id, $current_team),
                    'class'       => 'select_checkbox'
                    );
                echo form_hidden('select_rider['.$rider->rider_id.']',0);
                echo form_checkbox($data);
                ?>
            </td>
            <td class='centertext'>    
                <?php
                $data = array(
                    'name'        => 'replacement_rider['.$rider->rider_id.']',
                    'id'          => 'replacement_rider',
                    'value'       => '1',
                    'checked'     => in_array($rider->rider_id, $current_replacements),
                    'class'       => 'replacement_checkbox'
                    );
                echo form_hidden('replacement_rider['.$rider->rider_id.']',0);
                echo form_checkbox($data);
                ?>                
            </td>
          </tr>
           
          <?php  } ?>
          </tbody>
          </table>  
          <button type="submit" class="btn btn-success"><i class="icon-ok icon-white"></i> <?php echo lang('save');?></button>
        </div>
        <?php } ?>
      </div>
    </div> 
      

    <?php echo form_close();?>
    </div>
  </div>  
   
